Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure constants written correctly to LP/NL files #2953

Merged
merged 21 commits into from
Aug 23, 2023

Conversation

jsiirola
Copy link
Member

@jsiirola jsiirola commented Aug 17, 2023

Fixes #2927, Fixes #2928

Summary/Motivation:

The LP and NL writers were more flexible in the data types they accepted, but introduced cases where non-int/float data types were written out to the NL or LP files. This PR goes through and ensures that all numeric constants are mapped to either int or float being written out to the file.

Note that this restores the original behavior reported in the issue, but does not fix the slightly more egregious error of permitting the use of Boolean as a domain for (numeric) Constraints.

Changes proposed in this PR:

  • ensure all numeric constants are either int or float before writing them out to the LP/NL file
  • standardize processing of Var and Constraint lb/ubproperties to ensure that the returned vales are either native numeric types orNone. This also ensures that ``inf / -inf are mapped to None before returning them to the user
  • minor performance improvements (~2% for LP writer, NL writer, and model generation)
  • Add a test to exercise issues originally reported in Error with numpy type handling in writers #2927 and nl_writer does not cast Boolean variable values to [0, 1] #2928.

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@blnicho blnicho self-requested a review August 22, 2023 19:07
@@ -1478,10 +1499,28 @@ def _write_nl_expression(self, repn, include_const):
if include_const and repn.const:
# Add the constant to the NL expression. AMPL adds the
# constant as the second argument, so we will too.
nl = self.template.binary_sum + nl + (self.template.const % repn.const)
nl = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate what black did to this.

Copy link
Contributor

@mrmundt mrmundt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some thoughts about possible refactor opportunities, but they can be tabled.

@codecov
Copy link

codecov bot commented Aug 23, 2023

Codecov Report

Patch coverage: 97.42% and project coverage change: +0.05% 🎉

Comparison is base (8abb24b) 88.04% compared to head (219c505) 88.10%.
Report is 26 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2953      +/-   ##
==========================================
+ Coverage   88.04%   88.10%   +0.05%     
==========================================
  Files         768      769       +1     
  Lines       89260    89708     +448     
==========================================
+ Hits        78592    79034     +442     
- Misses      10668    10674       +6     
Flag Coverage Δ
linux 85.17% <97.42%> (+0.03%) ⬆️
osx 74.98% <94.84%> (+0.04%) ⬆️
other 85.35% <97.42%> (+0.21%) ⬆️
win 82.41% <97.42%> (+3.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
pyomo/repn/plugins/lp_writer.py 92.69% <86.95%> (-0.96%) ⬇️
pyomo/core/base/constraint.py 91.84% <96.00%> (-0.14%) ⬇️
pyomo/repn/plugins/nl_writer.py 94.02% <97.87%> (+0.38%) ⬆️
pyomo/common/dependencies.py 98.27% <100.00%> (+<0.01%) ⬆️
pyomo/common/unittest.py 97.94% <100.00%> (+0.01%) ⬆️
pyomo/core/base/var.py 93.85% <100.00%> (+0.25%) ⬆️
pyomo/core/kernel/constraint.py 100.00% <100.00%> (ø)
pyomo/core/kernel/matrix_constraint.py 100.00% <100.00%> (ø)
pyomo/core/kernel/variable.py 99.47% <100.00%> (+0.01%) ⬆️
pyomo/repn/util.py 99.15% <100.00%> (+0.03%) ⬆️

... and 19 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pyomo/core/base/var.py Outdated Show resolved Hide resolved
pyomo/core/base/var.py Outdated Show resolved Hide resolved
pyomo/core/base/var.py Outdated Show resolved Hide resolved
pyomo/core/base/var.py Outdated Show resolved Hide resolved
pyomo/core/base/var.py Outdated Show resolved Hide resolved
pyomo/core/base/var.py Outdated Show resolved Hide resolved
@mrmundt mrmundt merged commit 984586f into Pyomo:main Aug 23, 2023
30 checks passed
@jsiirola jsiirola deleted the lp-nl-cast-floats branch August 28, 2023 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nl_writer does not cast Boolean variable values to [0, 1] Error with numpy type handling in writers
3 participants